package com.xm.settlement.controller;

import com.xm.settlement.commons.base.BaseController;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.commons.utils.RedisBeanUtils;
import com.xm.settlement.model.AppBase;
import com.xm.settlement.model.vo.AppUserVo;
import com.xm.settlement.service.IAppBaseService;
import com.xm.settlement.service.ScorePoolService;
import com.xm.settlement.service.impl.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2017/4/21.
 */
@Controller
@RequestMapping(value = "/scorePool")
public class ScorePoolController extends BaseController {

    @Autowired
    private ScorePoolService scorePoolService;

    @Autowired
    private IAppBaseService appBaseService;

    @Autowired
    private RedisCacheUtil<AppUserVo> redisCacheUtil;

    /**
         * 动态奖金管理页
     *
     * @return
     */
    @GetMapping("/manager")
    public String manager() {
        return "settlement/scorePool/scorePool";
    }

    /**
     * 动态奖金列表
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        pageInfo.setCondition(condition);
        scorePoolService.selectDataGrid(pageInfo);
        return pageInfo;
    }

    /**
     * 现金池奖金分发
     * */
    @RequestMapping("/insertAvgScore")
    @ResponseBody
    public  Object insertAvgScore(Double awards){
        AtomicInteger flag = new AtomicInteger(0);
        AppBase appBase = redisCacheUtil.getCacheObject(RedisBeanUtils.APP_BASE_MAP+"_isLockScorePool_1");
        try {
            if (appBase  == null ) {
                appBase = new AppBase();
                appBase.setType("isLockScorePool");
                appBase.setKey("1");
                appBase = appBaseService.selectOne(appBase);
                appBase.setPercentage(1);
                appBaseService.updateById(appBase);
            }else {
                if(appBase.getPercentage() == 1 ) {
                    flag.set(1);
                } else {
                    appBase = new AppBase();
                    appBase.setType("isLockScorePool");
                    appBase.setKey("1");
                    appBase = appBaseService.selectOne(appBase);
                    if(appBase.getPercentage() == 1) { flag.set(1); }
                    appBase.setPercentage(1);
                    appBaseService.updateById(appBase);
                    redisCacheUtil.setCacheObject(RedisBeanUtils.APP_BASE_MAP + "_" + appBase.getType() + "_" + appBase.getKey(), appBase);
                }
            }
                appBase = appBaseService.selectOne(appBase);
                appBase.setPercentage(1);
                appBaseService.updateById(appBase);
                redisCacheUtil.del(RedisBeanUtils.APP_BASE_MAP + "_" + appBase.getType() + "_" + appBase.getKey());
                redisCacheUtil.setCacheObject(RedisBeanUtils.APP_BASE_MAP + "_" + appBase.getType() + "_" + appBase.getKey(), appBase);
                scorePoolService.insertAvgScore(awards);
            } catch (Exception e) {
                e.printStackTrace();
                return renderError(e.getMessage());
            } finally {
                if(flag.intValue() ==1 ){
                    appBase.setPercentage(0);
                    appBaseService.updateById(appBase);
                    redisCacheUtil.del(RedisBeanUtils.APP_BASE_MAP + "_" + appBase.getType() + "_" + appBase.getKey());
                    redisCacheUtil.setCacheObject(RedisBeanUtils.APP_BASE_MAP + "_" + appBase.getType() + "_" + appBase.getKey(), appBase);
                }
            }
        return  renderSuccess("返现成功!");
    }

    /**
     * 锁定增值点
     * @return
     */
    @RequestMapping("/lockIncrement")
    @ResponseBody
    public  Object lockIncrement(){
        try {
            AppBase appBase = new AppBase();
            appBase.setType("isLockScorePool");
            appBase.setKey("1");
            appBase = appBaseService.selectOne(appBase);
            appBase.setPercentage(1);
            appBaseService.updateById(appBase);
            redisCacheUtil.del(RedisBeanUtils.APP_BASE_MAP+"_"+appBase.getType()+"_"+appBase.getKey() );
            redisCacheUtil.setCacheObject(RedisBeanUtils.APP_BASE_MAP+"_"+appBase.getType()+"_"+appBase.getKey(),appBase);
        }catch (Exception e){
            e.printStackTrace();
            return renderError("锁定失败!");
        }
        return  renderSuccess("锁定成功!");
    }

    /**
     * 不锁定增值点
     * @return
     */
    @RequestMapping("/unlockIncrement")
    @ResponseBody
    public  Object unlockIncrement(){
        try {
            AppBase appBase = new AppBase();
            appBase.setType("isLockScorePool");
            appBase.setKey("1");
            appBase = appBaseService.selectOne(appBase);
            appBase.setPercentage(0);
            appBaseService.updateById(appBase);
            redisCacheUtil.del(RedisBeanUtils.APP_BASE_MAP+"_"+appBase.getType()+"_"+appBase.getKey() );
            redisCacheUtil.setCacheObject(RedisBeanUtils.APP_BASE_MAP+"_"+appBase.getType()+"_"+appBase.getKey(),appBase);
        }catch (Exception e){
            e.printStackTrace();
          return renderError("解锁失败!");
        }
        return  renderSuccess("解锁成功!");
    }

}
